﻿using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Common;
using DevelopTool.Model;
using DevelopTool.Model.CodeModels;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.LuckDrawModels;
using DevelopTool.Model.NoteModels;
using DevelopTool.Model.OffLineModels;
using LiteDB;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace DevelopTool.Bll.OffLineBlls
{
    /// <summary>
    /// 离线项操作类
    /// </summary>
    public class OffLineBll : BaseBll
    {

        public OffLineBll()
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<OffLine>();
                col.EnsureIndex(x => x.NodeName);
                col.EnsureIndex(x => x.NodePid);
                col.EnsureIndex(x => x.NodeType);
                col.EnsureIndex(x => x.CreateTime);
                col.EnsureIndex(x => x.Id);
            }
        }


        /// <summary>
        /// 获取离线项列表
        /// </summary>
        /// <returns></returns>
        public List<OffLine> GetList(MenuNodeType nodeType = MenuNodeType.Unknown)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var models = db.GetCollection<OffLine>().FindAll();

                if (nodeType != MenuNodeType.Unknown)
                {
                    models = models.Where(it => it.NodeType == nodeType);
                }

                return models.OrderByDescending(it => it.CreateTime).ToList();
            }
        }


        public OffLine GetByIdIgnoringData(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<OffLine>();

                var model = col.FindOne(it => it.Id == id);

                model.DataGridColumnsByte = null;
                model.DataTableByte = null;

                return model;
            }
        }


        public OffLine GetById(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<OffLine>();

                var offLine = col.FindOne(it => it.Id == id);

                if (offLine.DataTableByte != null && offLine.DataTableByte.Length > 0)
                {
                    offLine.DataTable = offLine.DataTableByte.ByteToObject_Decompress<DataTable>();

                    offLine.DataTableByte = null;//防止占内存
                }
                else
                {
                    offLine.DataTable = new DataTable();
                }

                if (offLine.DataGridColumnsByte != null && offLine.DataGridColumnsByte.Length > 0)
                {
                    offLine.DataGridColumns = offLine.DataGridColumnsByte.ByteToObject_Decompress<List<DataGridColumn>>();
                    offLine.DataGridColumnsByte = null;
                }
                else
                {
                    offLine.DataGridColumns = new List<DataGridColumn>();
                }

                return offLine;
            }
        }

        public OffLine Save(OffLine offLine)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<OffLine>();

                if (offLine.DataTable != null && offLine.DataTable.Rows.Count > 0)
                {
                    offLine.DataTableByte = offLine.DataTable.ObjectToByte_Compress();
                    offLine.DataTable = null;
                }
                if (offLine.DataGridColumns != null && offLine.DataGridColumns.Count > 0)
                {
                    offLine.DataGridColumnsByte = offLine.DataGridColumns.ObjectToByte_Compress();
                }

                offLine.UpdateTime = DateTime.Now;
                col.Upsert(offLine);

                return col.FindById(offLine.Id);
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            using (var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<OffLine>();

                //查询是否有子节点，若有子节点，则不删除。
                if (col.Exists(it => it.NodePid == id)) return false;

                //删除
                return col.Delete(id);
            }
        }

        public void SetRemark(int id,string remark)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<OffLine>();

                var model = col.FindOne(it => it.Id == id);

                model.Remark = remark;
                model.UpdateTime = DateTime.Now;

                col.Update(model);
            }
        }

        public ManageMenu Get(int id)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                return db.GetCollection<OffLine>().FindOne(it => it.Id == id);
            }
        }

    }
}
